//
//
//  管理界面(综合界面)
//
//

import QtQuick
import QtQuick.Controls.Material
import Qt5Compat.GraphicalEffects
Item {
    id: managerAndSettingWindowRoot
    width: rootWidth
    height: rootHeight /*- 150*/

    Component.onCompleted: {
        Cpp_PLC.QML_CurrentPage = 8;
        // Cpp_PLC.write_Modbus_RTU_Hold_16(48,"4")
    }

    A_Button{
        anchors.left: parent.left
        anchors.leftMargin: 50
        anchors.top: parent.top
        anchors.topMargin: 50
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("左 移")

        onButtonPressed: {
            // console.log("左移按下")
            Cpp_PLC.write_Modbus_RTU_Coils(21,"1")
        }

        onButtonReleased: {
            // console.log("左移松开")
            Cpp_PLC.write_Modbus_RTU_Coils(21,"0")
        }

    }

    A_Button{
        anchors.left: parent.left
        anchors.leftMargin: 415
        anchors.top: parent.top
        anchors.topMargin: 50
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("右 移")
        onButtonPressed: {
            // console.log("右移按下")
            Cpp_PLC.write_Modbus_RTU_Coils(22,"1")
        }

        onButtonReleased: {
            // console.log("右移松开")
            Cpp_PLC.write_Modbus_RTU_Coils(22,"0")
        }

    }

    A_Button{
        anchors.right: parent.right
        anchors.rightMargin: 50
        anchors.top: parent.top
        anchors.topMargin: 50
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("复 位")
        onButtonPressed: {
            // console.log("复位按下")
            Cpp_PLC.write_Modbus_RTU_Coils(5,"1")
        }

        onButtonReleased: {
            // console.log("复位松开")
            Cpp_PLC.write_Modbus_RTU_Coils(5,"0")
        }

    }


    A_Button{
        anchors.left: parent.left
        anchors.leftMargin: 50
        anchors.top: parent.top
        anchors.topMargin: 230
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        visible: false
        contentText_Data: qsTr("左机头 -45度")
        onButtonPressed: {
            console.log("左机头 -45度 1")
        }

        onButtonReleased: {
            console.log("左机头 -45度 0")
        }

    }

    A_Button{
        anchors.left: parent.left
        anchors.leftMargin: 415
        anchors.top: parent.top
        anchors.topMargin: 230
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("左机头 90度")
        contentRectangle_BorderWidth: Cpp_PLC.IO_Y5  ? 5 : 3
        contentRectangle_BorderColor: Cpp_PLC.IO_Y5  ? "gold" : "black"
        onButtonPressed: {
            // console.log("复位按下")
            Cpp_PLC.write_Modbus_RTU_Coils(15,"1")
        }

        onButtonReleased: {
            // console.log("复位松开")
            Cpp_PLC.write_Modbus_RTU_Coils(15,"0")
        }

    }

    A_Button{
        anchors.right: parent.right
        anchors.rightMargin: 50
        anchors.top: parent.top
        anchors.topMargin: 230
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("左机头 45度")
        contentRectangle_BorderWidth: Cpp_PLC.IO_Y4  ? 5 : 3
        contentRectangle_BorderColor: Cpp_PLC.IO_Y4  ? "gold" : "black"
        onButtonPressed: {
            // console.log("复位按下")
            Cpp_PLC.write_Modbus_RTU_Coils(13,"1")
        }

        onButtonReleased: {
            // console.log("复位松开")
            Cpp_PLC.write_Modbus_RTU_Coils(13,"0")
        }

    }

    A_Button{
        anchors.left: parent.left
        anchors.leftMargin: 50
        anchors.top: parent.top
        anchors.topMargin: 410
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        visible: false
        contentText_Data: qsTr("右机头 -45度")

        onButtonPressed: {
            console.log("右机头 -45度 1")
        }

        onButtonReleased: {
            console.log("右机头 -45度 0")
        }

    }

    A_Button{
        anchors.left: parent.left
        anchors.leftMargin: 415
        anchors.top: parent.top
        anchors.topMargin: 410
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("右机头 90度")
        contentRectangle_BorderWidth: Cpp_PLC.IO_Y7  ? 5 : 3
        contentRectangle_BorderColor: Cpp_PLC.IO_Y7  ? "gold" : "black"
        onButtonPressed: {
            // console.log("复位按下")
            Cpp_PLC.write_Modbus_RTU_Coils(16,"1")
        }

        onButtonReleased: {
            // console.log("复位松开")
            Cpp_PLC.write_Modbus_RTU_Coils(16,"0")
        }

    }

    A_Button{
        anchors.right: parent.right
        anchors.rightMargin: 50
        anchors.top: parent.top
        anchors.topMargin: 410
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("右机头 45度")
        contentRectangle_BorderWidth: Cpp_PLC.IO_Y6  ? 5 : 3
        contentRectangle_BorderColor: Cpp_PLC.IO_Y6  ? "gold" : "black"
        onButtonPressed: {
            // console.log("复位按下")
            Cpp_PLC.write_Modbus_RTU_Coils(14,"1")
        }

        onButtonReleased: {
            // console.log("复位松开")
            Cpp_PLC.write_Modbus_RTU_Coils(14,"0")
        }

    }

    A_Button{
        anchors.left: parent.left
        anchors.leftMargin: 50
        anchors.top: parent.top
        anchors.topMargin: 590
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("左锯片工进")
        contentRectangle_BorderWidth: Cpp_PLC.IO_Y10  ? 5 : 3
        contentRectangle_BorderColor: Cpp_PLC.IO_Y10  ? "gold" : "black"
        onButtonPressed: {
            // console.log("复位按下")
            Cpp_PLC.write_Modbus_RTU_Coils(17,"1")
        }

        onButtonReleased: {
            // console.log("复位松开")
            Cpp_PLC.write_Modbus_RTU_Coils(17,"0")
        }

    }

    A_Button{
        anchors.left: parent.left
        anchors.leftMargin: 415
        anchors.top: parent.top
        anchors.topMargin: 590
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("左电机启动")
        contentRectangle_BorderWidth: Cpp_PLC.IO_Y2  ? 5 : 3
        contentRectangle_BorderColor: Cpp_PLC.IO_Y2  ? "gold" : "black"
        onButtonPressed: {
            // console.log("复位按下")
            Cpp_PLC.write_Modbus_RTU_Coils(11,"1")
        }

        onButtonReleased: {
            // console.log("复位松开")
            Cpp_PLC.write_Modbus_RTU_Coils(11,"0")
        }

    }

    A_Button{
        anchors.right: parent.right
        anchors.rightMargin: 50
        anchors.top: parent.top
        anchors.topMargin: 590
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("左压料")
        contentRectangle_BorderWidth: Cpp_PLC.IO_Y12  ? 5 : 3
        // contentRectangle_BorderColor: Cpp_PLC.M_507 === 0 && Cpp_PLC.IO_Y12  ? "gold" : "black"
        contentRectangle_BorderColor: Cpp_PLC.M_507 === false && Cpp_PLC.IO_Y12 === true ? "gold" :
                                      Cpp_PLC.M_507 === false && Cpp_PLC.IO_Y12 === false ? "black" :
                                      Cpp_PLC.M_507 === true && Cpp_PLC.IO_Y12 === true ?  "black" :
                                                                                            "gold"
        onButtonPressed: {
            // console.log("复位按下")
            Cpp_PLC.write_Modbus_RTU_Coils(19,"1")
        }

        onButtonReleased: {
            // console.log("复位松开")
            Cpp_PLC.write_Modbus_RTU_Coils(19,"0")
        }

    }


    A_Button{
        anchors.left: parent.left
        anchors.leftMargin: 50
        anchors.top: parent.top
        anchors.topMargin: 770
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("右锯片工进")
        contentRectangle_BorderWidth: Cpp_PLC.IO_Y11  ? 5 : 3
        contentRectangle_BorderColor: Cpp_PLC.IO_Y11  ? "gold" : "black"
        onButtonPressed: {
            // console.log("复位按下")
            Cpp_PLC.write_Modbus_RTU_Coils(18,"1")
        }

        onButtonReleased: {
            // console.log("复位松开")
            Cpp_PLC.write_Modbus_RTU_Coils(18,"0")
        }

    }

    A_Button{
        anchors.left: parent.left
        anchors.leftMargin: 415
        anchors.top: parent.top
        anchors.topMargin: 770
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("右电机启动")
        contentRectangle_BorderWidth: Cpp_PLC.IO_Y3  ? 5 : 3
        contentRectangle_BorderColor: Cpp_PLC.IO_Y3  ? "gold" : "black"
        onButtonPressed: {
            // console.log("复位按下")
            Cpp_PLC.write_Modbus_RTU_Coils(12,"1")
        }

        onButtonReleased: {
            // console.log("复位松开")
            Cpp_PLC.write_Modbus_RTU_Coils(12,"0")
        }

    }

    A_Button{
        anchors.right: parent.right
        anchors.rightMargin: 50
        anchors.top: parent.top
        anchors.topMargin: 770
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("右压料")
        contentRectangle_BorderWidth: Cpp_PLC.IO_Y13  ? 5 : 3
        // contentRectangle_BorderColor: Cpp_PLC.IO_Y13  ? "gold" : "black"
        contentRectangle_BorderColor: Cpp_PLC.M_507 === false && Cpp_PLC.IO_Y13 === true ? "gold" :
                                      Cpp_PLC.M_507 === false && Cpp_PLC.IO_Y13 === false ? "black" :
                                      Cpp_PLC.M_507 === true && Cpp_PLC.IO_Y13 === true ?  "black" :
                                                                                            "gold"
        onButtonPressed: {
            // console.log("复位按下")
            Cpp_PLC.write_Modbus_RTU_Coils(20,"1")
        }

        onButtonReleased: {
            // console.log("复位松开")
            Cpp_PLC.write_Modbus_RTU_Coils(20,"0")
        }

    }

    A_Button{
        anchors.left: parent.left
        anchors.leftMargin: 50
        anchors.top: parent.top
        anchors.topMargin: 950
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("外摆拽料")
        contentRectangle_BorderWidth: Cpp_PLC.IO_Y21  ? 5 : 3
        contentRectangle_BorderColor: Cpp_PLC.IO_Y21  ? "gold" : "black"
        onButtonPressed: {
            // console.log("复位按下")
            Cpp_PLC.write_Modbus_RTU_Coils(171,"1")
        }

        onButtonReleased: {
            // console.log("复位松开")
            Cpp_PLC.write_Modbus_RTU_Coils(171,"0")
        }
    }


    A_Button{
        anchors.left: parent.left
        anchors.leftMargin: 415
        anchors.top: parent.top
        anchors.topMargin: 950
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("左侧拉料")
        contentRectangle_BorderWidth: Cpp_PLC.IO_Y23  ? 5 : 3
        contentRectangle_BorderColor: Cpp_PLC.IO_Y23  ? "gold" : "black"
        onButtonPressed: {
            // console.log("复位按下")
            Cpp_PLC.write_Modbus_RTU_Coils(198,"1")
        }

        onButtonReleased: {
            // console.log("复位松开")
            Cpp_PLC.write_Modbus_RTU_Coils(198,"0")
        }
    }

    A_Button{
        anchors.right: parent.right
        anchors.rightMargin: 50
        anchors.top: parent.top
        anchors.topMargin: 950
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("读取电机位置")
        onButtonPressed: {
            // console.log("复位按下")
            Cpp_PLC.write_Modbus_RTU_Coils(53,"1")
            Cpp_PLC.write_Modbus_RTU_Coils(147,"1")

        }

        onButtonReleased: {
            // console.log("复位松开")
            Cpp_PLC.write_Modbus_RTU_Coils(53,"0")
        }
    }


    A_Button{
        anchors.left: parent.left
        anchors.leftMargin: 50
        anchors.top: parent.top
        anchors.topMargin: 1130
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("吹气阀")
        onButtonPressed: {
            // console.log("复位按下")
            Cpp_PLC.write_Modbus_RTU_Coils(235,"1")
        }

        onButtonReleased: {
            // console.log("复位松开")
            Cpp_PLC.write_Modbus_RTU_Coils(235,"0")
        }
    }


    A_Button{
        anchors.left: parent.left
        anchors.leftMargin: 415
        anchors.top: parent.top
        anchors.topMargin: 1130
        // contentText_Fontsize: 30
        buttonWidth: 250
        buttonHeight: 90
        contentText_Data: qsTr("料高气缸")
        contentRectangle_BorderWidth: Cpp_PLC.IO_Y14  ? 5 : 3
        contentRectangle_BorderColor: Cpp_PLC.IO_Y14  ? "gold" : "black"
        onButtonPressed: {
            // console.log("复位按下")
            Cpp_PLC.write_Modbus_RTU_Coils(101,"1")
        }

        onButtonReleased: {
            // console.log("复位松开")
            Cpp_PLC.write_Modbus_RTU_Coils(101,"0")
        }
    }

    Item{
        anchors.top: managerAndSettingWindowRoot.top
        anchors.topMargin: 1300
        anchors.horizontalCenter: managerAndSettingWindowRoot.horizontalCenter
        width: managerAndSettingWindowRoot.width
        height: 170
        visible: Cpp_PLC.AutoObserveState

        A_Label{
            anchors.top: parent.top
            // anchors.topMargin:
            anchors.horizontalCenter: parent.horizontalCenter
            labelWidth: 300
            label_Data: qsTr("料高测量值")
            labelBorderColor: "transparent"
            labelBack: "transparent"
        }

        A_Label{
            anchors.top: parent.top
            anchors.topMargin: 70
            anchors.horizontalCenter: parent.horizontalCenter
            labelBorderColor: "#F0F8FF"
            labelWidth: 300
            labelBack: "black"
            label_FontColor: "aqua"
            label_Data: Cpp_PLC.MaterialHeightValue
        }

        A_Button{
            anchors.top: parent.top
            anchors.topMargin: 70
            anchors.left: parent.left
            anchors.leftMargin: 120

            contentText_Data: qsTr("误差修正")
            // contentText_Fontsize: 40

            onButtonPressed: {
                // console.log("误差修正按下")
                Cpp_PLC.write_Modbus_RTU_Coils(110,"1")
            }

            onButtonReleased: {
                // console.log("误差修正松开")
            }
        }

        Rectangle{
            anchors.top: parent.top
            anchors.topMargin: 70
            anchors.right: parent.right
            anchors.rightMargin: 320
            width: 30
            height: 70
            radius: 10
            border.width: 1
            border.color: "#F0F8FF"
            // color: "black"
            color: Cpp_PLC.ObservingState ? "green" : "black"

        }

        A_Button{
            anchors.top: parent.top
            anchors.topMargin: 70
            anchors.right: parent.right
            anchors.rightMargin: 120
            contentText_Data: qsTr("测  高")
            // contentText_Fontsize: 40

            // onButtonPressed: {
            //     console.log("测高按钮按下")
            // }

            // onButtonReleased: {
            //     console.log("测高按钮松开")
            // }
            onButtonPressed: {
                // console.log("测高按钮按下")
                Cpp_PLC.write_Modbus_RTU_Coils(108,"1")
            }

            onButtonReleased: {
                // console.log("测高按钮松开")
                Cpp_PLC.write_Modbus_RTU_Coils(108,"0")
            }
        }
    }


    ComboBox {
        id: language_ComboBox
        anchors.top: parent.top
        anchors.topMargin: 1580
        anchors.left: parent.left
        anchors.leftMargin: 30
        width : 300
        height: 70
        currentIndex: Cpp_Config.Language_Type-1
        visible: true

        // 模型数据
        model: ListModel {
            id: comboModel
            ListElement{name:qsTr("中文")}
            ListElement{name:qsTr("英文")}
            ListElement{name:qsTr("越南文")}
        }

        property string customDisplayText: model.get(currentIndex).name

        // 显示文本覆盖
        contentItem: Text {
            text: language_ComboBox.customDisplayText
            font.pixelSize: 26
            color: language_ComboBox.currentIndex >= 0 &&
                   language_ComboBox.model.get(language_ComboBox.currentIndex).premium ?
                       premiumColor : "#2c3e50"
            horizontalAlignment: Text.AlignLeft
            verticalAlignment: Text.AlignVCenter
            leftPadding: 20
            elide: Text.ElideRight
            font.bold: true
        }

        // 背景样式
        background: Rectangle {
            // color: "#ffffff"
            // border.color: language_ComboBox.hovered ? "#3498db" : "#bdc3c7"
            border.width: 3
            radius: 12

            // 当ComboBox打开时改变样式
            color: language_ComboBox.popup.visible ? "#f0f8ff" : "#ffffff"
            border.color: language_ComboBox.popup.visible ? "#3498db" : (language_ComboBox.hovered ? "#3498db" : "#bdc3c7")
        }

        // 下拉指示器
        indicator: Canvas {
            x: language_ComboBox.width - width - 20
            y: language_ComboBox.topPadding + (language_ComboBox.availableHeight - height) / 2
            width: 24
            height: 14
            contextType: "2d"

            onPaint: {
                context.reset();
                context.moveTo(0, 0);
                context.lineTo(width, 0);
                context.lineTo(width / 2, height);
                context.closePath();
                context.fillStyle = language_ComboBox.popup.visible ? "#3498db" : "#7f8c8d";
                context.fill();
            }
        }

        // 下拉菜单（高度200px）
        popup: Popup {
            y: language_ComboBox.height + 10
            width: language_ComboBox.width
            implicitHeight: 200
            // padding: 2

            contentItem: ListView {
                clip: true
                implicitHeight: contentHeight
                model: language_ComboBox.popup.visible ? language_ComboBox.delegateModel : null
                currentIndex: language_ComboBox.highlightedIndex
                boundsBehavior: Flickable.StopAtBounds
            }

            background: Rectangle {
                color: "#ffffff"
                border.color: "#3498db"
                border.width: 2
                radius: 12
                layer.enabled: true
                layer.effect: DropShadow {
                    radius: 12
                    color: "#40000000"
                    samples: 20
                }
            }
        }

        // 下拉项委托
        delegate: ItemDelegate {
            width: language_ComboBox.width
            height: 60
            // hoverEnabled: true

            // 名称（大字体）
            Text {
                text: model.name
                font.pixelSize: 36
                color: "#2c3e50"
                font.bold: true
                anchors.verticalCenter:  parent.verticalCenter
                anchors.left: parent.left
                anchors.leftMargin: 20
            }

        }


        onActivated: {
            // console.log(model.get(currentIndex).name)
            Cpp_Language.CurrentType = currentIndex+1
            Cpp_Config.Language_Type = currentIndex+1
            Cpp_PLC.CurrentLanguageType = currentIndex+1
            if(currentIndex+1 === 1)
            {
                Cpp_Printer.Language_Type = true
                console.log(Cpp_Printer.Language_Type)
            }
            else
            {
                Cpp_Printer.Language_Type = false
                console.log(Cpp_Printer.Language_Type)
            }
            Cpp_Language.load(Cpp_Language.CurrentType)
        }

    }

}
